package com.fubowen.reader.adapter

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.children
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.fubowen.reader.databinding.PublicTreeItemBinding
import com.fubowen.reader.db.model.Tree


class SwipeCallback() : ItemTouchHelper.Callback() {

    private val duration = 300L
    private var initX = 0f
    private var lastX = 0f
    private var isLeftLimit: Boolean = false
    private var menuWidth = 0f
    private val temp = mutableSetOf<ViewHolder>()

    //region 固定设置
    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: ViewHolder): Int {
        return makeMovementFlags(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
    }

    override fun onMove(recyclerView: RecyclerView, viewHolder: ViewHolder, target: ViewHolder) = false

    override fun onSwiped(viewHolder: ViewHolder, direction: Int) {}

    override fun getSwipeThreshold(viewHolder: ViewHolder) = 1f

    override fun getSwipeEscapeVelocity(defaultValue: Float) = Float.MAX_VALUE

    override fun getAnimationDuration(recyclerView: RecyclerView, animationType: Int, animateDx: Float, animateDy: Float) = duration
    //endregion

    override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
//        val view = viewHolder.itemView
//        if (isCurrentlyActive) {
//            isLeftLimit = dX <= -viewHolder.itemView.width * getSwipeThreshold(viewHolder)
//        }
//        val menu = view.findViewById<TextView>(id)
//        if (isLeftLimit) {
//            menu.text = "松开删除"
//        } else {
//            menu.text = "继续滑动"
//        }
//        if (view is ViewGroup) {
//            view.children.forEach { it.translationX = dX }
//        }
    }

    override fun clearView(recyclerView: RecyclerView, viewHolder: ViewHolder) {
        super.clearView(recyclerView, viewHolder)
        recyclerView.postDelayed({
            initX = if (isLeftLimit) -menuWidth else 0f
        }, 50)
    }

    private fun drawMenu(c: Canvas, left: Int, top: Int, bottom: Int) {
        drawMenuItem(c, "删除", left, top, bottom)
    }

    private fun drawMenuItem(c: Canvas, text: String, left: Int, top: Int, bottom: Int): Int {
        val bounds = Rect()
        val textPaint = Paint()
        textPaint.color = Color.WHITE
        textPaint.textSize = 14 * 3.0f
        textPaint.isAntiAlias = true
        textPaint.getTextBounds(text, 0, text.length, bounds)

        val paddingLeft = 16 * 3
        val paddingRight = 16 * 3

        val width = paddingLeft + bounds.width() + paddingRight

        val right = left + width
        val rect = Rect(left, top, right, bottom)
        val background = ColorDrawable(Color.RED)
        background.bounds = rect
        background.draw(c)

        c.drawText(text, (left + paddingLeft).toFloat(), rect.centerY().toFloat(), textPaint)
        return right
    }

    private fun calcTextWidth(text: String): Float {
        val bounds = Rect()
        val textPaint = Paint()
        textPaint.color = Color.WHITE
        textPaint.textSize = 14 * 3.0f
        textPaint.isAntiAlias = true
        textPaint.getTextBounds(text, 0, text.length, bounds)

        val paddingLeft = 16 * 3f
        val paddingRight = 16 * 3

        return paddingLeft + bounds.width() + paddingRight
    }

    fun restore(currentList: MutableList<Tree>, recycler: RecyclerView) {
//        val showMenuList = currentList.filter { it.status == Tree.STATUS_SHOW_MENU }
//        repeat(showMenuList.size) {
//            val view = recycler.layoutManager?.findViewByPosition(it)
//            val vh = recycler.getChildViewHolder(view!!)
//            val itemViewCanvas = Canvas(view!!.getDrawingCache())
//        }
//        val dd = temp
//        val ddd = temp
    }
}